﻿namespace Autorent.Desktop.Modules.Addresses.Services
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Autorent.Desktop.Model;
    using Autorent.Desktop.Model.AutorentMngrServiceReference;


    public class RegionsRepository : AutorentEntitiesContext, IRegionsRepository
    {
        public ObservableCollection<Region> GetRegions()
        {
            try
            {
                return new ObservableCollection<Region>(Context.Regions);
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void AddRegion(Region region)
        {
            Context.AddToRegions(region);
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while connecting service", ex.InnerException);
            }
        }

        public void UpdateRegion(Region region)
        {
            Context.UpdateObject(region);
            Context.SaveChanges();
        }

        public void DeleteRegion(Region region)
        {
            try
            {
                Context.DeleteObject(Context.Regions.Where(r => r.Region_ID.Equals(region.Region_ID)));
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new AutorentModelException("Error occured while deleting object", ex.InnerException);
            }
        }
    }
}
